package com.hy.frame.recycler;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import java.util.HashSet;
import java.util.Set;

public abstract class BaseItemDecoration extends RecyclerView.ItemDecoration {
    private int divider;
    @Nullable
    private Drawable mDrawable;
    @Nullable
    private Paint mPaint;
    @NonNull
    private final Rect mPadding;
    private boolean mStartDivider;
    private boolean mEndDivider;
    @NonNull
    private final Set<Integer> mTypes;

    public BaseItemDecoration(int divider) {
        this(divider, Color.TRANSPARENT);
    }

    public BaseItemDecoration(int divider, int color) {
        this(divider, color, null);
    }

    public BaseItemDecoration(int divider, @Nullable Drawable drawable) {
        this(divider, Color.TRANSPARENT, drawable);
    }

    private BaseItemDecoration(int divider, int color, @Nullable Drawable drawable) {
        this.divider = divider;
        if (color != Color.TRANSPARENT) {
            this.mPaint = new Paint();
            this.mPaint.setColor(color);
            this.mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        } else {
            this.mPaint = null;
        }
        this.mDrawable = drawable;
        this.mPadding = new Rect();
        this.mTypes = new HashSet<>();
    }

    public BaseItemDecoration setDivider(int divider) {
        this.divider = divider;
        return this;
    }

    public int getDivider() {
        return divider;
    }

    public void setDrawable(@Nullable Drawable drawable) {
        this.mDrawable = drawable;
    }

    @Nullable
    public Drawable getDrawable() {
        return mDrawable;
    }

    public void setColor(int color) {
        if (color != Color.TRANSPARENT) {
            this.mPaint = new Paint();
            this.mPaint.setColor(color);
            this.mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        } else {
            this.mPaint = null;
        }
    }

    @Nullable
    public Paint getPaint() {
        return mPaint;
    }

    public void setTop(int top) {
        this.mPadding.top = top;
    }

    public void setLeft(int left) {
        this.mPadding.left = left;
    }

    public void setRight(int right) {
        this.mPadding.right = right;
    }

    public void setBottom(int bottom) {
        this.mPadding.bottom = bottom;
    }

    public BaseItemDecoration setPadding(int left, int top, int right, int bottom) {
        this.mPadding.set(left, top, right, bottom);
        return this;
    }

    @NonNull
    public Rect getPadding() {
        return mPadding;
    }

    public boolean isStartDivider() {
        return mStartDivider;
    }

    /**
     * 第一条顶部是否需要添加分割线
     */
    public void setStartDivider(boolean enable) {
        this.mStartDivider = enable;
    }

    public boolean isEndDivider() {
        return mEndDivider;
    }

    /**
     * 最后一条底部是否需要添加分割线
     */
    public void setEndDivider(boolean enable) {
        this.mEndDivider = enable;
    }

    public void addType(int type) {
        this.mTypes.add(type);
    }

    public void removeType(int type) {
        this.mTypes.remove(type);
    }

    public boolean containsType(int type) {
        if (this.mTypes.size() == 0 && type == 0)
            return true;
        return this.mTypes.contains(type);
    }
    //    public BaseItemDecoration build() {
//        return this;
//    }

    /**
     * onDraw是在itemView绘制之前
     *
     * @param c      Canvas
     * @param parent RecyclerView
     * @param state  RecyclerView.State
     */
    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        //super.onDraw(c, parent, state);
        if (parent.getAdapter() == null) return;
        if (getPaint() == null && getDrawable() == null) return; //Paint和Drawable都未定义
        if (getPaint() != null && getPaint().getAlpha() == 0) return; //Paint透明时
        if (getDrawable() != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && getDrawable().getAlpha() == 0) return; //Android 4.4 并且Drawable透明时
        drawDivider(c, parent);
    }

    /**
     * onDrawOver是在itemView绘制之后
     *
     * @param c      Canvas
     * @param parent RecyclerView
     * @param state  RecyclerView.State
     */
    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {

    }

    /**
     * 绘制分割线
     */
    public void drawRectDivider(@NonNull Canvas canvas, int left, int top, int right, int bottom) {
        if (getDrawable() != null) {
            getDrawable().setBounds(left, top, right, bottom);
            getDrawable().draw(canvas);
        } else if (getPaint() != null) {
            canvas.drawRect(left, top, right, bottom, getPaint());
        }
    }

    /**
     * 配置Item边距
     * @param outRect 边距
     * @param view Item控件
     * @param parent 宿主
     * @param state 列表状态
     */
    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        if (parent.getAdapter() == null)
            outRect.set(0, 0, 0, 0);
        else
            configureItemOutRect(outRect, view, parent);
    }

    protected abstract void drawDivider(@NonNull Canvas canvas, @NonNull RecyclerView parent);

    protected abstract void configureItemOutRect(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent);
}
